home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13446 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: vixen.cso.uiuc.edu!usenet
  2. From: e-torres@uiuc.edu (Edgar L. Torres)
  3. Newsgroups: comp.lang.c++
  4. Subject: HELP: Overloading [] operator for multidimensional array of objects.
  5. Date: Mon, 25 Mar 1996 18:39:38 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4j6p2v$dhg@vixen.cso.uiuc.edu>
  8. NNTP-Posting-Host: berlin-4.slip.uiuc.edu
  9. Summary: How does one specify overloading the []operator for array of objects?
  10. Keywords: overload, subscript, multidimensional, arrays, operator
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. Hi!
  14.  
  15.     I would like to specify the overload of [], such that I may access
  16. a multidimensional array of objects. As an example, given I have a
  17. class
  18. EXAMCLASS and I wish to dynamically allocate a three-dimensional array
  19. of
  20. objects:
  21.     EXAMCLASS object1***;
  22.     ...
  23.     object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
  24. I know the above is sacrilege to C++ purists, but bare with me, since
  25. the malloc
  26. does work correctly in C++.  Now, what I want is to be able to:
  27.     object1[arg1][arg2][arg3].obj1Var1 = something;
  28. OR
  29.     something = object1[arg1][arg2][arg3].obj1Var1;
  30. In general, I know I have to include three friend operator in the
  31. definition of
  32. class EXAMCLASS, and then define them outside:
  33.     EXAMCLASS** operator[](EXAMCLASS*** C, int i){
  34.         return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
  35.     EXAMCLASS* operator[](EXAMCLASS** C, int i){
  36.         return ((EXAMCLASS *)(C + (i * dim3sz))); }
  37.     EXAMCLASS operator[](EXAMCLASS* C, int i){
  38.         return *(C + i); }
  39. I know that the above does not work, but it does present the basic
  40. idea of what
  41. I am trying to do.  If anybody has the knowledge to help with the
  42. specifics of 
  43. syntax and argument rules, please HELP ME!  Somebody suggested
  44. replacing the *
  45. with &, but I am not sure what EXAMCLASS&& means??
  46.  
  47. Thanks in advance,
  48. Edgar
  49.  
  50.  
  51.